# -*- coding: utf-8 -*-
"""
Created on Thu Dec 12 21:41:31 2013

@author: daimoon
"""
import numpy as np
from math import *
import pylab as plt
t = lambda txt: txt.encode('cp1251').decode('utf8')
def graf_prantl(phi, b):
    """
    Построение линий скольжения для решения Прандтля
    """
    deg2rad = lambda x: x * (pi / 180)
    rad2deg = lambda x: int(x * (180 / pi))
#    phi = deg2rad(phi)
    array_out_x = np.zeros((0))
    array_out_y = np.zeros((0))
    angle_1 = (pi / 4) + (phi / 2)
    angle_2 = angle_1 + (pi / 2)
    angle_3 = (pi / 4) - (phi / 2)

    add = lambda x, ar: np.append(ar, x)
    e_in_ang = lambda ang, phi: e ** (ang * tan(phi))
    array_out_x, array_out_y = add(0.5 * b, array_out_x), add(0, array_out_y)
    x = 0.5 * b
    y = -tan(angle_1) * 0.5 * b
    array_out_x, array_out_y = add(x, array_out_x), add(y, array_out_y)
    for i in range(rad2deg(angle_1), rad2deg(angle_2), 1):
        C = b / (2 * cos(angle_1) * e_in_ang(angle_1 ,phi))
        r = C * e_in_ang(deg2rad(i) ,phi)
        x = r * cos(deg2rad(i))
        y = -r * sin(deg2rad(i))
        array_out_x, array_out_y = add(x, array_out_x), add(y, array_out_y)
    n_end = array_out_y.size - 1
    x = 2 * array_out_y[n_end] / tan(angle_3)
    y = 0
    array_out_x, array_out_y = add(x, array_out_x), add(y, array_out_y)
    return array_out_x * (-1), array_out_y

if __name__ == '__main__':
    phi = 30
    b = 1

    phi = phi * (pi / 180)
    xl, yl = graf_prantl(phi, b)
    one = [b * 0.5 for i in range(xl.size)]
    xl = xl + one
    max_x = max(xl.flatten())
    max_y = -min(yl.flatten())
    plt.plot(xl, yl, linewidth=1, label=t('Решение Прандтля'))
    plt.xlim (-max_x * 0.1, max_x * 1.1)
    plt.ylim (-max_y * 1.1, max_y * 0.1)
    plt.legend(loc='best')
    plt.draw()


